package httputil

import (
	"errors"
	"playbook/ginsvr/src/global"
	"playbook/ginsvr/src/orm"
	"playbook/ginsvr/src/projectmanagement"

	"github.com/sirupsen/logrus"
	"gopkg.in/h2non/gentleman.v2"
	"gopkg.in/h2non/gentleman.v2/plugins/body"
)

type Scriptdata struct {
	IP  string
	Act *orm.Activity
}

type Projectdata struct {
	IP      string
	Project *projectmanagement.Project
}

type Srcfile struct {
	Srcpath string
	Flst    []string
}

type CopyContent struct {
	IP      string
	Newpath []string
	Oldpath []string
}

type Sgresp struct {
	Head    orm.Response `json:"bizhead"`
	Srcpath string       `json:"srcpath"`
}

func PublicScriptCrt(sd Scriptdata) (pubpath string, err error) {
	lp := orm.GetLocalPath("public")
	cli := gentleman.New()
	cli.URL("http://" + lp.LocalIp + ":8080/scriptGen")

	cli.Use(body.JSON(sd))

	req := cli.Request()
	req.Method("POST")

	res, err := req.Send()
	if err != nil {
		global.LOG.WithFields(logrus.Fields{
			"err": err,
		}).Error("public scriptGen err.")
		return pubpath, err
	}
	global.LOG.Info(res.String())

	if !res.Ok {
		global.LOG.WithFields(logrus.Fields{
			"code": res.StatusCode,
		}).Error("Invalid server response.")
		return pubpath, errors.New("Invalid server response")
	}
	var sgresp Sgresp
	res.JSON(&sgresp)
	pubpath = sgresp.Srcpath

	return pubpath, nil
}

func FileReWrite(ip string, srcpath string, flst []string) (err error) {

	cli := gentleman.New()
	cli.URL("http://" + ip + ":8080/fileReWrite")

	var srcfile Srcfile
	srcfile.Srcpath = srcpath
	srcfile.Flst = flst

	cli.Use(body.JSON(srcfile))

	req := cli.Request()
	req.Method("POST")

	res, err := req.Send()
	if err != nil {
		global.LOG.WithFields(logrus.Fields{
			"err": err,
		}).Error("local file re-write err.")
		return err
	}

	if !res.Ok {
		global.LOG.WithFields(logrus.Fields{
			"code": res.StatusCode,
		}).Error("Invalid server response.")
		return errors.New("Invalid server response")
	}

	return nil
}

func ScriptCopy(content CopyContent) (err error) {
	lp := orm.GetLocalPath("public")
	cli := gentleman.New()
	cli.URL("http://" + lp.LocalIp + ":8080/scriptcopy")

	cli.Use(body.JSON(content))

	req := cli.Request()
	req.Method("POST")

	res, err := req.Send()
	if err != nil {
		global.LOG.WithFields(logrus.Fields{
			"err": err,
		}).Error("local file clear err.")
		return err
	}

	if !res.Ok {
		global.LOG.WithFields(logrus.Fields{
			"code": res.StatusCode,
		}).Error("Invalid server response.")
		return errors.New("Invalid server response")
	}

	return nil
}

func FileDel(ip string, srcpath string) (err error) {
	cli := gentleman.New()
	cli.URL("http://" + ip + ":8080/filedelete")

	var srcfile Srcfile
	srcfile.Srcpath = srcpath

	cli.Use(body.JSON(srcfile))

	req := cli.Request()
	req.Method("POST")

	res, err := req.Send()
	if err != nil {
		global.LOG.WithFields(logrus.Fields{
			"err": err,
		}).Error("local file clear err.")
		return err
	}

	if !res.Ok {
		global.LOG.WithFields(logrus.Fields{
			"code": res.StatusCode,
		}).Error("Invalid server response.")
		return errors.New("Invalid server response")
	}

	return nil
}

func PublicProjectCrt(sd Projectdata) (res *gentleman.Response, err error) {
	// 测试使用，提交top时修改成public
	lp := orm.GetLocalPath("public")
	cli := gentleman.New()
	cli.URL("http://" + lp.LocalIp + ":8080/addProject")

	cli.Use(body.JSON(sd))

	req := cli.Request()
	req.Method("POST")

	res, err = req.Send()
	if err != nil {
		global.LOG.WithFields(logrus.Fields{
			"err": err,
		}).Error("public projectGen err.")
		return res, err
	}
	global.LOG.Info(res.String())

	if !res.Ok {
		global.LOG.WithFields(logrus.Fields{
			"code": res.StatusCode,
		}).Error("Invalid server response.")
		return res, errors.New("Invalid server response")
	}
	return res, nil
}

func PullProjectFormServer(sd Projectdata) (res *gentleman.Response, err error) {
	// 测试使用，提交top时修改成public
	lp := orm.GetLocalPath("public")
	cli := gentleman.New()
	cli.URL("http://" + lp.LocalIp + ":8080/getServerProjectList")

	cli.Use(body.JSON(sd))

	req := cli.Request()
	req.Method("POST")

	res, err = req.Send()
	if err != nil {
		global.LOG.WithFields(logrus.Fields{
			"err": err,
		}).Error("pull project form server err.")
		return res, err
	}
	global.LOG.Info(res.String())

	if !res.Ok {
		global.LOG.WithFields(logrus.Fields{
			"code": res.StatusCode,
		}).Error("Invalid server response.")
		return res, errors.New("Invalid server response")
	}

	return res, nil
}
